package com.example.homework;

public class Calculator {
        private Double numFirst;
        private Double numSecond;
        private CalculatorOperator operator;
        private String currentNum;
        private boolean pressDot = false;
        public Calculator() {
//        调用一次
            clear();
        }
        //    AC清除
        private void clear() {
            numFirst = 0.0;
            numSecond = 0.0;
            operator = null;
            currentNum = "";
        }
        //    判断数值类型 将浮点数转换为长整型运算
        private String DoubleToInt(Double number) {
            boolean flag = number.longValue() == number;
            if (flag) {
                return String.valueOf(number.longValue());
            }
            return number.toString();
        }
        //    判断是第一个数还是第二个
        public String clickData(CalculatorData data) {
            Double number = null;
            if (this.operator == null) {
                number = numFirst;
            } else {
                number = numSecond;
            }
            String numberText = currentNum + data.getNumber();
            try {
                number  = Double.parseDouble(numberText);
            } catch (NumberFormatException e) {
                numberText = currentNum;
            }
            if (operator == null) {
                numFirst = number;
            } else {
                numSecond = number;
            }
//        未输入操作符之前拼接数值
            StringBuilder builder = new StringBuilder();
            if (operator == null) {
                builder.append(numberText);
            } else {
                builder.append(DoubleToInt(numFirst));
                builder.append(operator.getOperator());
                builder.append(numberText);
            }
            currentNum = numberText;
            return builder.toString();
        }
        //    定义逻辑运算操作
        public String clickOperator(CalculatorOperator operator, String numberText) {
            String result = "";
            switch (operator) {
                case ADD:
                case SUB:
                case MU:
                case DIV:
                    char inputOperator = operator.getOperator();
                    if (this.operator == null) {
                        result = numberText + inputOperator;
                    } else {
                        char lastOperator = numberText.charAt(numberText.length());
                        result = equals(numberText);
                        if(this.operator == null) {
                            result += inputOperator;
                        } else {
                            result = result.replace(lastOperator, inputOperator);
                        }
                    }
                    currentNum = "";
                    this.operator = operator;
                    break;
                case CLEAR:
                    clear();
                    result = "";
                    break;
                case PERCENT:
                    result = pointer();
                    break;
                case EQUALS:
                    result = equals(numberText);
                    break;
            }
            return result;
        }
        //        取百分比
        private String pointer() {
            numFirst /= 100;
            operator = null;
            return  DoubleToInt(numFirst);
        }
        //    加减乘除（等号运算结果）
        private String equals(String numberText) {
            if (this.operator == null || numSecond.equals(0.0)) {
                return numberText;
            }
            double result = 0;
            switch (operator) {
                case ADD:
                    result = numFirst + numSecond;
                    break;
                case SUB:
                    result = numFirst = numSecond;
                    break;
                case MU:
                    result = numFirst * numSecond;
                    break;
                case DIV:
                    result = numFirst / numSecond;
            }
            numFirst = result;
            operator = null;
            numSecond = 0.0;
            currentNum = DoubleToInt(result);
            return DoubleToInt(numFirst);
        }

    }


